// pages/operation/classRecord/classMakeupStudent/index.js
import { ClassModel } from "../../../../api/api-work/classModel.js"
import Toast from '@vant/weapp/toast/toast';
const classModel = new ClassModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    // 添加补课学员
    makeUpParam: {
      tid: "",
      cofId: "",
      classDate: "",
      orderNo: "",
    },
    courseId: "",
    courseName: "", // 课程名称
    lessonType: "",
    pageNum: 1,
    total: 0,
    tableDatas: [],
    triggered: false,
    loadStatus: "",
    showDianming: true, // 显示点名按钮  默认true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const param = JSON.parse(options.param)
    console.log("nana: ", param)
    if (param.lessonType) {
      this.setData({
        courseId: param.id,
        courseName: param.name,
        lessonType: param.lessonType,
        showDianming: true
      })
    }
    else {
      this.setData({
        courseId: param.id,
        'makeUpParam.cofId': param.cofId,
        'makeUpParam.tid': param.tid,
        'makeUpParam.orderNo': param.orderNo,
        'makeUpParam.classDate': param.classDate,
        showDianming: false
      })
    }

    this.getOrderDatas()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  async getOrderDatas() {
    classModel.getOrderListSecondApi({ pageNum: this.data.pageNum, pageSize: 15, listType: 3, courseId: this.data.courseId, orderNo: this.data.makeUpParam.orderNo }).then(res => {

      if (res.rows && res.rows.length > 0) {
        res.rows.forEach(item => {
          item['classDateNew'] = item.classDate.split(" ")[0];
          item["checked"] = false;
        })
      }
      let rows = this.data.tableDatas;
      rows.push(...res.rows);
      this.setData({
        tableDatas: rows,
        total: res.total
      })

      if (this.data.total == 0) {
        this.setData({
          loadStatus: ""
        })
      }
      else if (this.data.tableDatas.length < this.data.total) {
        this.setData({
          loadStatus: "loadmore"
        })
      }
      else {
        this.setData({
          loadStatus: "nomore"
        })
      }
    })
  },
  getMoreDatas() {
    if (this.data.loadStatus === "nomore") {
      return;
    }
    this.setData({
      pageNum: this.data.pageNum + 1,
      loadStatus: "loading"
    })
    this.getOrderDatas()
  },
  refreshMoreDatas() {
    this.setData({
      triggered: false,
      pageNum: 1,
      tableDatas: []
    })
    this.getOrderDatas()
  },

  // 选择学员
  checkedStudent(e) {
    const value = e.currentTarget.dataset.query;
    this.data.tableDatas.forEach(item => {
      if (item.id === value.id) {
        item.checked = !item.checked;
      }
    })
    this.setData({
      tableDatas: this.data.tableDatas
    })
  },

  // 去点名
  jumpToLessonInfo() {
    let selectStudents = [];
    this.data.tableDatas.forEach(item => {
      if (item.checked) {
        selectStudents.push(item)
      }
    })
    if (selectStudents.length == 0) {
      Toast('请选择补课学员');
    }
    else {
      classModel.saveRepairApi({ courseId: this.data.courseId, courseName: this.data.courseName, isVip: this.data.lessonType, tempOrderNo: this.data.makeUpParam.orderNo }).then(res => {
        const obj = {
          selectStudents: selectStudents,
          courseId: this.data.courseId,
          courseName: this.data.courseName,
          lessonType: this.data.lessonType,
          classHour: res.classHour,
          className: res.name,
          classId: res.id,
        }
        wx.navigateTo({
          url: `/pages/operation/classRecord/classRecordInfo/index?param=${JSON.stringify(obj)}`,
        })
      })

    }
  },

  // 添加补课学员
  confirmAddStudent() {
    let selectStudents = [];
    this.data.tableDatas.forEach(item => {
      if (item.checked) {
        selectStudents.push(item)
      }
    })
    if (selectStudents.length == 0) {
      Toast('请选择补课学员');
    } else {
      console.log("haha: ", this.data, selectStudents)

      let arr = [];
      let arrIds = [];
      this.data.tableDatas.forEach(item => {
        if (item.checked) {
          const obj = {
            id: item.studentId,
            orderNo: item.orderNo,
            courseId: item.courseId
          }
          arrIds.push(item.studentId);
          arr.push(obj)
        }
      })
      classModel.addStudentApi({ classDate: `${this.data.makeUpParam.classDate} 00:00:00`, cofId: this.data.makeUpParam.cofId, temp: 2, orderNo: this.data.makeUpParam.orderNo, ids: arr }).then(res => {
        classModel.saveOrderUpdateApi({ tid: this.data.makeUpParam.tid, type: 3, studentIds: arrIds }).then(resp => {
          wx.navigateBack()
        })

      })
      return;
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})